1. Field of the Invention
The present invention relates to optimization of computer code. More specifically, the present invention relates to a method and an apparatus for optimizing safe downcasting and similar programming idioms in an object-oriented language.
2. Related Art
Computer programs written in modern computing languages, and particularly programs written in object-oriented languages, typically include conventions and idioms, which are designed to make the source code more readable and easier to understand by a programmer and to make the executable code more robust during execution. While these conventions and idioms are useful, they typically introduce inefficiencies into the executable code for the program.
Table 1 illustrates an unsafe cast operation that casts a pointer from type “base *” to derived type “A *.” Note that this code will cast the pointer “p” to type “A*” regardless of whether the object pointed to by “p” is of type “A*,” type “B*,” or type “BB*.”
TABLE 1class base {};class A:base {};class B:base {};class BB:B {};foo (base * p) { A * ap; ap = (A*)p;}
Safe downcasting is a particularly useful idiom for ensuring that casting operations on an object of a base type to an object of a derived type are proper. Table 2 illustrates the safe downcasting idiom.
In the safe downcasting idiom, the base class (“base” in this example) includes a virtual function for each derived class of the base class. These virtual functions each return a NULL pointer. In each derived class, the virtual function for that derived class is defined to return the “this” pointer for the object, while the virtual function for the other derived classes inherit the NULL pointer return from the base class. Since the compiler is necessarily aware of the type of the object pointed to by “p”, calling the “as_X( )” method of a derived class “X” returns a NULL pointer if the object is not of that derived class, and a “this” pointer if the object is of that derived class. Note that “as_X” implies calling the “as_A,” “as_B,” or “as_BB” function as required. Thus, the safe downcasting idiom ensures that the correct downcast is used for subsequent code.
As an example, the function “foo” uses an “if-else” structure to determine the proper derived type of the pointer “p.” The statement:
“if ((ap = p→as_A( )) != NULL)”
assigns “this” to ap if p points to an “A” object. Otherwise, the statement assigns NULL to ap. The test for “not equal to NULL” can then be used to determine whether to use the code for an object of derived type “A.” Likewise, the statements:
“if ((bp = p→as_B( )) != NULL)”                and “if ((bbp = p→as_BB( )) != NULL)”can be used to determine if the object is of derived type “B” or “BB,” respectively.        
TABLE 2class A;class B;class BB;class base {public:  virtual class A * as_A ( ){return NULL; }  virtual class B * as_B ( ){return NULL; }  virtual class BB * as_BB ( ){return NULL; }};class A:base {  virtual class A * as_A ( ) {return this; }  // note as_B returning NULL is inherited from base by default  // note as_BB returning NULL is inherited from base by default};class B:base {  virtual class B * as_B ( ) {return this; }  // note as_A returning NULL is inherited from base by default  // note as_BB returning NULL is inherited from base by default};class BB:base {  virtual class BB * as_BB ( ) {return this; }  // note as_B returning this is inherited from class B by default  // note as_A returning NULL is inherited from base by default};foo (base * p) { class A * ap; class B * bp; class BB * bbp; if ((ap = p->as_A ( )) != NULL) {  // use ap } else if ((bp = p->as_B ( )) != NULL) {  if ((bbp = p->as_BB ( )) != NULL) {   // use bbp  } else {   // use bp  } }}
This safe downcasting idiom can guarantee that the proper code is used for the derived type of the object and uses the following steps:
fetch the virtual function table pointer from p→;
fetch the “as_X” function pointer from the virtual function table;
indirectly call the “as_X” function via this pointer;                a. the result is set to NULL, or set to “this,” depending on which virtual function got called;        b. return from the function; and        
compare the function result with the NULL pointer.
This series of steps, particularly the indirect function call and its associated return, is expensive in terms of the number of machine cycles required.
Hence, what is needed is a method and an apparatus for optimizing safe downcasting and similar programming idioms in an object-oriented programming language to reduce the number of required machine cycles.